import api from "../../../common/api.js";
import util from "../../../utils/util.js";
var settingInfo = wx.getStorageSync("settingInfo") ? wx.getStorageSync("settingInfo") : {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArray: [],
    buCarid:''
  },
  //选择图片
  chooseimage: function() {
    var that = this;
    util.updateImages(1, function (res) {
      var tempFilePaths = res.tempFilePaths[0];
      util.uploadFiles(tempFilePaths, function (res) {
        try {
          var id = JSON.parse(res.data)[0].id;
          that.setData({
            imgArray: tempFilePaths,
            imageId: id
          })
          //保存微信二维码
          api.settingEdit({
            wechatQrCode: id
          }, function (res) {
            settingInfo.wechatInfoState = true;
            wx.setStorageSync("setState", true);
            wx.setStorageSync("settingInfo", settingInfo);
            
          })
          
        } catch (error) {
          
        }
      })
    })
  },
  //预览名片
  previewImage: function(e) {
    var arr = [];
    arr.push(this.data.imgArray)
    wx.previewImage({
      urls: arr,
    })
  },
  //保存名片
  saveWechatInfo() {
    var that = this;
    if (that.data.imageId) {
      api.settingEdit({
        wechatQrCode: that.data.imageId
      }, function(res) {
        settingInfo.wechatInfoState = true;
        wx.setStorageSync("setState", true);
        wx.setStorageSync("settingInfo", settingInfo);
        wx.navigateBack({
          delta: 1
        })
      })
    } else {
      if (that.data.buCarid == ''){
        wx.showToast({
          title: '请选择您要上传的微信二维码',
          icon: 'none',
        })
      }else{
        wx.navigateBack({
          delta: 1
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
 
    if (options.wechatQrCode == "undefined" || options.wechatQrCode == undefined || options.wechatQrCode == "null"){
      options.wechatQrCode = ""
    }
    this.setData({
      imgArray: options.wechatQrCode,
      buCarid: options.wechatQrCode,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  }
})